package com.hrt.sequence;

import java.lang.annotation.Annotation;
import java.sql.SQLException;

import javax.persistence.Table;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.Sequence;

public class SequenceDaoImpl extends BaseDaoImpl<Sequence>{

public String getSequence(Class clz){
	
	String tableName=getTableName(clz);
	if(tableName!=null){
	final String hql="select seq from Sequence seq where seq.tableName="+tableName+" and seq.isdelete=0";
	return this.getHibernateTemplate().execute(new HibernateCallback<String>(){

		@Override
		public String doInHibernate(Session session) throws HibernateException,
				SQLException {
			Query query=session.createQuery(hql);
			return	(String) query.uniqueResult();
			
		}
		});
	}else{
		return null;
	}
	
		
	}


/**
 * 获取表名称
 * @param clz
 * @return
 */
private String getTableName(Class clz)
{
	
	
	Annotation	tabs=clz.getAnnotation(Table.class);
	try {
		Object obj= clz.newInstance();
		Object tableName=tabs.getClass().getField("name").get(obj);
		if(tableName!=null){
			String table=tableName.toString();
			return table;
		}
	} catch (IllegalArgumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InstantiationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
}
